package com.example.androidtraining_ahut2020;

import android.app.Activity;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

public class MainActivityDay11SQLiteDemo extends Activity {
    private ListView listView;
    private SimpleAdapter simpleAdapter = null;
    private LinearLayout loadLayout = null;    // 读取的脚标的视图
    private TextView loadInfo = null;    // 进行信息提示
    private List<Map<String, Object>> all = null;
    private LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);    // 表示新组件的布局参数
    private SQLiteOpenHelper helper = null;
    private LinearLayout mylayout = null;
    private int currentPage = 1;
    private int lineSize = 15;
    private int allRecorders = 0;
    private int pageSize = 1;    // 默认在一共只有1页
    private int lastItem = 0;    // 保存最后一项
    private Button insertBut = null;
    private Button updateBut = null;
    private Button deleteBut = null;
    private MytabOperate mtab = null;
    private static int count = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main_day11_sqlite);
        this.mylayout = (LinearLayout) super.findViewById(R.id.mylayout);
        this.loadLayout = new LinearLayout(this);    // 定义脚标的线性布局管理器
        this.loadInfo = new TextView(this);    // 文本组件
        this.loadInfo.setText("数据加载中ing...");    // 定义提示文字
        this.loadInfo.setGravity(Gravity.CENTER);    // 文字居中显示
        this.loadInfo.setTextSize(30.0f);    // 文字大小
        this.loadLayout.addView(this.loadInfo, this.layoutParams);    // 增加组件
        this.loadLayout.setGravity(Gravity.CENTER);
        this.showAllData();    // 数据显示
        this.pageSize = (this.allRecorders + this.lineSize - 1)
                / this.lineSize; // 计算总页数
        System.out.println("pageSize = " + this.pageSize);
        System.out.println("allRecorders = " + this.allRecorders);
        this.insertBut = (Button) super.findViewById(R.id.insertBut);
        this.updateBut = (Button) super.findViewById(R.id.updateBut);
        this.deleteBut = (Button) super.findViewById(R.id.deleteBut);
        this.insertBut.setOnClickListener(new InsertOnClickListenerImpl());
        this.updateBut.setOnClickListener(new UpdateOnClickListenerImpl());
        this.deleteBut.setOnClickListener(new DeleteOnClickListenerImpl());
    }

    private class InsertOnClickListenerImpl implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            MainActivityDay11SQLiteDemo.this.mtab = new MytabOperate(
                    MainActivityDay11SQLiteDemo.this.helper.getWritableDatabase());
            MainActivityDay11SQLiteDemo.this.mtab.insert("孙悟空" + count++, "1979-08-12");
        }

    }

    private class UpdateOnClickListenerImpl implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            MainActivityDay11SQLiteDemo.this.mtab = new MytabOperate(
                    MainActivityDay11SQLiteDemo.this.helper.getWritableDatabase());
            MainActivityDay11SQLiteDemo.this.mtab.update(3, "猪八戒", "1981-06-27");
        }

    }

    private class DeleteOnClickListenerImpl implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            MainActivityDay11SQLiteDemo.this.mtab = new MytabOperate(
                    MainActivityDay11SQLiteDemo.this.helper.getWritableDatabase());
            MainActivityDay11SQLiteDemo.this.mtab.delete(3);

        }

    }

    private void showAllData() {    // 读取全部的数据
        MainActivityDay11SQLiteDemo.this.helper = new MyDatabaseHelper(MainActivityDay11SQLiteDemo.this);
        MytabCursor cur = new MytabCursor(    // 实例化查询
                MainActivityDay11SQLiteDemo.this.helper.getReadableDatabase());    // 取得SQLiteDatabase对象
        this.allRecorders = cur.getCount();    // 取得全部记录数
        this.listView = new ListView(MainActivityDay11SQLiteDemo.this);
        MainActivityDay11SQLiteDemo.this.all = cur.find(MainActivityDay11SQLiteDemo.this.currentPage, MainActivityDay11SQLiteDemo.this.lineSize);
        this.listView.addFooterView(this.loadLayout);    // 增加读取数据的布局文件
        this.simpleAdapter = new SimpleAdapter(MainActivityDay11SQLiteDemo.this,    // 上下文对象
                MainActivityDay11SQLiteDemo.this.all,            // 所有要操作的数据
                R.layout.activity_main_day11_tab_info,                // 布局管理器
                new String[]{"id", "name", "birthday"},    // map中的key
                new int[]{R.id.id, R.id.name, R.id.birthday});
        this.listView.setAdapter(this.simpleAdapter);    // 布局管理中的id
        this.listView.setOnScrollListener(new OnScrollListenerImpl());
        this.mylayout.addView(listView);
    }

    private class OnScrollListenerImpl implements OnScrollListener {

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                             int visibleItemCount, int totalItemCount) {
            MainActivityDay11SQLiteDemo.this.lastItem = firstVisibleItem + visibleItemCount - 1;    // 统计是否到最后
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (MainActivityDay11SQLiteDemo.this.lastItem == MainActivityDay11SQLiteDemo.this.simpleAdapter
                    .getCount()// 已经是最底部
                    && MainActivityDay11SQLiteDemo.this.currentPage < MainActivityDay11SQLiteDemo.this.pageSize    // 还有数据没读取完
                    && scrollState == OnScrollListener.SCROLL_STATE_IDLE) {    // 不再滑动
                MainActivityDay11SQLiteDemo.this.currentPage++;
                MainActivityDay11SQLiteDemo.this.listView.setSelection(MainActivityDay11SQLiteDemo.this.lastItem);    // 设置显示位置
                MainActivityDay11SQLiteDemo.this.appendData();    // 增加数据
            }
        }

    }

    private void appendData() {    // 增加数据
        MytabCursor cur = new MytabCursor(    // 实例化查询
                MainActivityDay11SQLiteDemo.this.helper.getReadableDatabase());    // 取得SQLiteDatabase对象
        List<Map<String, Object>> newData = cur.find(this.currentPage,
                this.lineSize);
        this.all.addAll(newData);    // 集合改变
        this.simpleAdapter.notifyDataSetChanged();    // 通知记录改变
    }
}